package com.ukee.birthday.common.utils;

import sun.misc.BASE64Encoder;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据处理
 *
 */
public class StrUtils {

    /**
     * String[] 转 Long[]
     * @param str  字符串
     * @return  返回Long[]
     */
	public static Long[] stringToLong(String str) {
        String[] arry = str.split(",");
        List<Long> list=new ArrayList<>();
        for (String s: arry) {
            try {
                list.add(Long.parseLong(s));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        Long[] longArr = list.toArray(new Long[list.size()]);

        return longArr;
    }

    public static String HMACSHA256(String data, String key) throws Exception {

        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");

        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");

        sha256_HMAC.init(secret_key);

        byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));

        return new BASE64Encoder().encode(array);
    }
}
